<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>13.5. tarfile — Read and write tar archive files &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="13. Data Compression and Archiving" href="archiving.html" />
    <link rel="next" title="14. File Formats" href="fileformats.html" />
    <link rel="prev" title="13.4. zipfile — Work with ZIP archives" href="zipfile.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="fileformats.html" title="14. File Formats"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="13.4. zipfile — Work with ZIP archives"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" accesskey="U">13. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-tarfile">
<span id="tarfile-mod"></span><h1>13.5. <tt class="xref docutils literal"><span class="pre">tarfile</span></tt> &#8212; Read and write tar archive files<a class="headerlink" href="#module-tarfile" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
<p>The <tt class="xref docutils literal"><span class="pre">tarfile</span></tt> module makes it possible to read and write tar
archives, including those using gzip or bz2 compression.
(<tt class="docutils literal"><span class="pre">.zip</span></tt> files can be read and written using the <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a> module.)</p>
<p>Some facts and figures:</p>
<ul>
<li><p class="first">reads and writes <a title="Interfaces for gzip compression and decompression using file objects." class="reference external" href="gzip.html#module-gzip"><tt class="xref docutils literal"><span class="pre">gzip</span></tt></a> and <a title="Interface to compression and decompression routines compatible with bzip2." class="reference external" href="bz2.html#module-bz2"><tt class="xref docutils literal"><span class="pre">bz2</span></tt></a> compressed archives.</p>
</li>
<li><p class="first">read/write support for the POSIX.1-1988 (ustar) format.</p>
</li>
<li><p class="first">read/write support for the GNU tar format including <em>longname</em> and <em>longlink</em>
extensions, read-only support for the <em>sparse</em> extension.</p>
</li>
<li><p class="first">read/write support for the POSIX.1-2001 (pax) format.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</li>
<li><p class="first">handles directories, regular files, hardlinks, symbolic links, fifos,
character devices and block devices and is able to acquire and restore file
information like timestamp, access permissions and owner.</p>
</li>
</ul>
<dl class="function">
<dt id="tarfile.open">
<tt class="descclassname">tarfile.</tt><tt class="descname">open</tt><big>(</big><em>name=None</em>, <em>mode='r'</em>, <em>fileobj=None</em>, <em>bufsize=10240</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#tarfile.open" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> object for the pathname <em>name</em>. For detailed
information on <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> objects and the keyword arguments that are
allowed, see <a class="reference internal" href="#tarfile-objects"><em>TarFile Objects</em></a>.</p>
<p><em>mode</em> has to be a string of the form <tt class="docutils literal"><span class="pre">'filemode[:compression]'</span></tt>, it defaults
to <tt class="docutils literal"><span class="pre">'r'</span></tt>. Here is a full list of mode combinations:</p>
<table border="1" class="docutils">
<colgroup>
<col width="29%" />
<col width="71%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">mode</th>
<th class="head">action</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'r'</span> <span class="pre">or</span> <span class="pre">'r:*'</span></tt></td>
<td>Open for reading with transparent
compression (recommended).</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r:'</span></tt></td>
<td>Open for reading exclusively without
compression.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r:gz'</span></tt></td>
<td>Open for reading with gzip compression.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r:bz2'</span></tt></td>
<td>Open for reading with bzip2 compression.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'a'</span> <span class="pre">or</span> <span class="pre">'a:'</span></tt></td>
<td>Open for appending with no compression. The
file is created if it does not exist.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w'</span> <span class="pre">or</span> <span class="pre">'w:'</span></tt></td>
<td>Open for uncompressed writing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w:gz'</span></tt></td>
<td>Open for gzip compressed writing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w:bz2'</span></tt></td>
<td>Open for bzip2 compressed writing.</td>
</tr>
</tbody>
</table>
<p>Note that <tt class="docutils literal"><span class="pre">'a:gz'</span></tt> or <tt class="docutils literal"><span class="pre">'a:bz2'</span></tt> is not possible. If <em>mode</em> is not suitable
to open a certain (compressed) file for reading, <a title="tarfile.ReadError" class="reference internal" href="#tarfile.ReadError"><tt class="xref docutils literal"><span class="pre">ReadError</span></tt></a> is raised. Use
<em>mode</em> <tt class="docutils literal"><span class="pre">'r'</span></tt> to avoid this.  If a compression method is not supported,
<a title="tarfile.CompressionError" class="reference internal" href="#tarfile.CompressionError"><tt class="xref docutils literal"><span class="pre">CompressionError</span></tt></a> is raised.</p>
<p>If <em>fileobj</em> is specified, it is used as an alternative to a file object opened
for <em>name</em>. It is supposed to be at position 0.</p>
<p>For special purposes, there is a second format for <em>mode</em>:
<tt class="docutils literal"><span class="pre">'filemode|[compression]'</span></tt>.  <a title="tarfile.open" class="reference internal" href="#tarfile.open"><tt class="xref docutils literal"><span class="pre">tarfile.open()</span></tt></a> will return a <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a>
object that processes its data as a stream of blocks.  No random seeking will
be done on the file. If given, <em>fileobj</em> may be any object that has a
<tt class="xref docutils literal"><span class="pre">read()</span></tt> or <tt class="xref docutils literal"><span class="pre">write()</span></tt> method (depending on the <em>mode</em>). <em>bufsize</em>
specifies the blocksize and defaults to <tt class="docutils literal"><span class="pre">20</span> <span class="pre">*</span> <span class="pre">512</span></tt> bytes. Use this variant
in combination with e.g. <tt class="docutils literal"><span class="pre">sys.stdin</span></tt>, a socket file object or a tape
device. However, such a <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> object is limited in that it does
not allow to be accessed randomly, see <a class="reference internal" href="#tar-examples"><em>Examples</em></a>.  The currently
possible modes:</p>
<table border="1" class="docutils">
<colgroup>
<col width="23%" />
<col width="77%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Mode</th>
<th class="head">Action</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'r|*'</span></tt></td>
<td>Open a <em>stream</em> of tar blocks for reading
with transparent compression.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r|'</span></tt></td>
<td>Open a <em>stream</em> of uncompressed tar blocks
for reading.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r|gz'</span></tt></td>
<td>Open a gzip compressed <em>stream</em> for
reading.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'r|bz2'</span></tt></td>
<td>Open a bzip2 compressed <em>stream</em> for
reading.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w|'</span></tt></td>
<td>Open an uncompressed <em>stream</em> for writing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w|gz'</span></tt></td>
<td>Open an gzip compressed <em>stream</em> for
writing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'w|bz2'</span></tt></td>
<td>Open an bzip2 compressed <em>stream</em> for
writing.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="class">
<dt id="tarfile.TarFile">
<em class="property">
class </em><tt class="descclassname">tarfile.</tt><tt class="descname">TarFile</tt><a class="headerlink" href="#tarfile.TarFile" title="Permalink to this definition">¶</a></dt>
<dd>Class for reading and writing tar archives. Do not use this class directly,
better use <a title="tarfile.open" class="reference internal" href="#tarfile.open"><tt class="xref docutils literal"><span class="pre">tarfile.open()</span></tt></a> instead. See <a class="reference internal" href="#tarfile-objects"><em>TarFile Objects</em></a>.</dd></dl>

<dl class="function">
<dt id="tarfile.is_tarfile">
<tt class="descclassname">tarfile.</tt><tt class="descname">is_tarfile</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#tarfile.is_tarfile" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if <em>name</em> is a tar archive file, that the <tt class="xref docutils literal"><span class="pre">tarfile</span></tt>
module can read.</dd></dl>

<dl class="class">
<dt id="tarfile.TarFileCompat">
<em class="property">
class </em><tt class="descclassname">tarfile.</tt><tt class="descname">TarFileCompat</tt><big>(</big><em>filename</em>, <em>mode='r'</em>, <em>compression=TAR_PLAIN</em><big>)</big><a class="headerlink" href="#tarfile.TarFileCompat" title="Permalink to this definition">¶</a></dt>
<dd><p>Class for limited access to tar archives with a <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a>-like interface.
Please consult the documentation of the <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a> module for more details.
<em>compression</em> must be one of the following constants:</p>
<dl class="data">
<dt id="tarfile.TarFileCompat.TAR_PLAIN">
<tt class="descname">TAR_PLAIN</tt><a class="headerlink" href="#tarfile.TarFileCompat.TAR_PLAIN" title="Permalink to this definition">¶</a></dt>
<dd>Constant for an uncompressed tar archive.</dd></dl>

<dl class="data">
<dt id="tarfile.TarFileCompat.TAR_GZIPPED">
<tt class="descname">TAR_GZIPPED</tt><a class="headerlink" href="#tarfile.TarFileCompat.TAR_GZIPPED" title="Permalink to this definition">¶</a></dt>
<dd>Constant for a <a title="Interfaces for gzip compression and decompression using file objects." class="reference external" href="gzip.html#module-gzip"><tt class="xref docutils literal"><span class="pre">gzip</span></tt></a> compressed tar archive.</dd></dl>

<p>
<span class="versionmodified">Deprecated since version 2.6: </span>The <a title="tarfile.TarFileCompat" class="reference internal" href="#tarfile.TarFileCompat"><tt class="xref docutils literal"><span class="pre">TarFileCompat</span></tt></a> class has been deprecated for removal in Python 3.0.</p>
</dd></dl>

<dl class="exception">
<dt id="tarfile.TarError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">TarError</tt><a class="headerlink" href="#tarfile.TarError" title="Permalink to this definition">¶</a></dt>
<dd>Base class for all <tt class="xref docutils literal"><span class="pre">tarfile</span></tt> exceptions.</dd></dl>

<dl class="exception">
<dt id="tarfile.ReadError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">ReadError</tt><a class="headerlink" href="#tarfile.ReadError" title="Permalink to this definition">¶</a></dt>
<dd>Is raised when a tar archive is opened, that either cannot be handled by the
<tt class="xref docutils literal"><span class="pre">tarfile</span></tt> module or is somehow invalid.</dd></dl>

<dl class="exception">
<dt id="tarfile.CompressionError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">CompressionError</tt><a class="headerlink" href="#tarfile.CompressionError" title="Permalink to this definition">¶</a></dt>
<dd>Is raised when a compression method is not supported or when the data cannot be
decoded properly.</dd></dl>

<dl class="exception">
<dt id="tarfile.StreamError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">StreamError</tt><a class="headerlink" href="#tarfile.StreamError" title="Permalink to this definition">¶</a></dt>
<dd>Is raised for the limitations that are typical for stream-like <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a>
objects.</dd></dl>

<dl class="exception">
<dt id="tarfile.ExtractError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">ExtractError</tt><a class="headerlink" href="#tarfile.ExtractError" title="Permalink to this definition">¶</a></dt>
<dd>Is raised for <em>non-fatal</em> errors when using <a title="tarfile.TarFile.extract" class="reference internal" href="#tarfile.TarFile.extract"><tt class="xref docutils literal"><span class="pre">TarFile.extract()</span></tt></a>, but only if
<tt class="xref docutils literal"><span class="pre">TarFile.errorlevel</span></tt><tt class="docutils literal"><span class="pre">==</span> <span class="pre">2</span></tt>.</dd></dl>

<dl class="exception">
<dt id="tarfile.HeaderError">
<em class="property">
exception </em><tt class="descclassname">tarfile.</tt><tt class="descname">HeaderError</tt><a class="headerlink" href="#tarfile.HeaderError" title="Permalink to this definition">¶</a></dt>
<dd><p>Is raised by <a title="tarfile.TarInfo.frombuf" class="reference internal" href="#tarfile.TarInfo.frombuf"><tt class="xref docutils literal"><span class="pre">TarInfo.frombuf()</span></tt></a> if the buffer it gets is invalid.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<p>Each of the following constants defines a tar archive format that the
<tt class="xref docutils literal"><span class="pre">tarfile</span></tt> module is able to create. See section <a class="reference internal" href="#tar-formats"><em>Supported tar formats</em></a> for
details.</p>
<dl class="data">
<dt id="tarfile.USTAR_FORMAT">
<tt class="descclassname">tarfile.</tt><tt class="descname">USTAR_FORMAT</tt><a class="headerlink" href="#tarfile.USTAR_FORMAT" title="Permalink to this definition">¶</a></dt>
<dd>POSIX.1-1988 (ustar) format.</dd></dl>

<dl class="data">
<dt id="tarfile.GNU_FORMAT">
<tt class="descclassname">tarfile.</tt><tt class="descname">GNU_FORMAT</tt><a class="headerlink" href="#tarfile.GNU_FORMAT" title="Permalink to this definition">¶</a></dt>
<dd>GNU tar format.</dd></dl>

<dl class="data">
<dt id="tarfile.PAX_FORMAT">
<tt class="descclassname">tarfile.</tt><tt class="descname">PAX_FORMAT</tt><a class="headerlink" href="#tarfile.PAX_FORMAT" title="Permalink to this definition">¶</a></dt>
<dd>POSIX.1-2001 (pax) format.</dd></dl>

<dl class="data">
<dt id="tarfile.DEFAULT_FORMAT">
<tt class="descclassname">tarfile.</tt><tt class="descname">DEFAULT_FORMAT</tt><a class="headerlink" href="#tarfile.DEFAULT_FORMAT" title="Permalink to this definition">¶</a></dt>
<dd>The default format for creating archives. This is currently <a title="tarfile.GNU_FORMAT" class="reference internal" href="#tarfile.GNU_FORMAT"><tt class="xref docutils literal"><span class="pre">GNU_FORMAT</span></tt></a>.</dd></dl>

<p>The following variables are available on module level:</p>
<dl class="data">
<dt id="tarfile.ENCODING">
<tt class="descclassname">tarfile.</tt><tt class="descname">ENCODING</tt><a class="headerlink" href="#tarfile.ENCODING" title="Permalink to this definition">¶</a></dt>
<dd>The default character encoding i.e. the value from either
<a title="sys.getfilesystemencoding" class="reference external" href="sys.html#sys.getfilesystemencoding"><tt class="xref docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a> or <a title="sys.getdefaultencoding" class="reference external" href="sys.html#sys.getdefaultencoding"><tt class="xref docutils literal"><span class="pre">sys.getdefaultencoding()</span></tt></a>.</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a></dt>
<dd>Documentation of the <a title="Read and write ZIP-format archive files." class="reference external" href="zipfile.html#module-zipfile"><tt class="xref docutils literal"><span class="pre">zipfile</span></tt></a> standard module.</dd>
<dt><a class="reference external" href="http://www.gnu.org/software/tar/manual/html_node/Standard.html">GNU tar manual, Basic Tar Format</a></dt>
<dd>Documentation for tar archive files, including GNU tar extensions.</dd>
</dl>
</div>
<div class="section" id="tarfile-objects">
<span id="id1"></span><h2>13.5.1. TarFile Objects<a class="headerlink" href="#tarfile-objects" title="Permalink to this headline">¶</a></h2>
<p>The <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> object provides an interface to a tar archive. A tar
archive is a sequence of blocks. An archive member (a stored file) is made up of
a header block followed by data blocks. It is possible to store a file in a tar
archive several times. Each archive member is represented by a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a>
object, see <a class="reference internal" href="#tarinfo-objects"><em>TarInfo Objects</em></a> for details.</p>
<dl class="class">
<dt>
<em class="property">
class </em><tt class="descclassname">tarfile.</tt><tt class="descname">TarFile</tt><big>(</big><em>name=None</em>, <em>mode='r'</em>, <em>fileobj=None</em>, <em>format=DEFAULT_FORMAT</em>, <em>tarinfo=TarInfo</em>, <em>dereference=False</em>, <em>ignore_zeros=False</em>, <em>encoding=ENCODING</em>, <em>errors=None</em>, <em>pax_headers=None</em>, <em>debug=0</em>, <em>errorlevel=0</em><big>)</big></dt>
<dd><p>All following arguments are optional and can be accessed as instance attributes
as well.</p>
<p><em>name</em> is the pathname of the archive. It can be omitted if <em>fileobj</em> is given.
In this case, the file object&#8217;s <tt class="xref docutils literal"><span class="pre">name</span></tt> attribute is used if it exists.</p>
<p><em>mode</em> is either <tt class="docutils literal"><span class="pre">'r'</span></tt> to read from an existing archive, <tt class="docutils literal"><span class="pre">'a'</span></tt> to append
data to an existing file or <tt class="docutils literal"><span class="pre">'w'</span></tt> to create a new file overwriting an existing
one.</p>
<p>If <em>fileobj</em> is given, it is used for reading or writing data. If it can be
determined, <em>mode</em> is overridden by <em>fileobj</em>&#8216;s mode. <em>fileobj</em> will be used
from position 0.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><em>fileobj</em> is not closed, when <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> is closed.</p>
</div>
<p><em>format</em> controls the archive format. It must be one of the constants
<a title="tarfile.USTAR_FORMAT" class="reference internal" href="#tarfile.USTAR_FORMAT"><tt class="xref docutils literal"><span class="pre">USTAR_FORMAT</span></tt></a>, <a title="tarfile.GNU_FORMAT" class="reference internal" href="#tarfile.GNU_FORMAT"><tt class="xref docutils literal"><span class="pre">GNU_FORMAT</span></tt></a> or <a title="tarfile.PAX_FORMAT" class="reference internal" href="#tarfile.PAX_FORMAT"><tt class="xref docutils literal"><span class="pre">PAX_FORMAT</span></tt></a> that are
defined at module level.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
<p>The <em>tarinfo</em> argument can be used to replace the default <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> class
with a different one.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
<p>If <em>dereference</em> is <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>, add symbolic and hard links to the archive. If it
is <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>, add the content of the target files to the archive. This has no
effect on systems that do not support symbolic links.</p>
<p>If <em>ignore_zeros</em> is <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>, treat an empty block as the end of the archive.
If it is <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>, skip empty (and invalid) blocks and try to get as many members
as possible. This is only useful for reading concatenated or damaged archives.</p>
<p><em>debug</em> can be set from <tt class="docutils literal"><span class="pre">0</span></tt> (no debug messages) up to <tt class="docutils literal"><span class="pre">3</span></tt> (all debug
messages). The messages are written to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>.</p>
<p>If <em>errorlevel</em> is <tt class="docutils literal"><span class="pre">0</span></tt>, all errors are ignored when using <a title="tarfile.TarFile.extract" class="reference internal" href="#tarfile.TarFile.extract"><tt class="xref docutils literal"><span class="pre">TarFile.extract()</span></tt></a>.
Nevertheless, they appear as error messages in the debug output, when debugging
is enabled.  If <tt class="docutils literal"><span class="pre">1</span></tt>, all <em>fatal</em> errors are raised as <a title="exceptions.OSError" class="reference external" href="exceptions.html#exceptions.OSError"><tt class="xref docutils literal"><span class="pre">OSError</span></tt></a> or
<a title="exceptions.IOError" class="reference external" href="exceptions.html#exceptions.IOError"><tt class="xref docutils literal"><span class="pre">IOError</span></tt></a> exceptions. If <tt class="docutils literal"><span class="pre">2</span></tt>, all <em>non-fatal</em> errors are raised as
<a title="tarfile.TarError" class="reference internal" href="#tarfile.TarError"><tt class="xref docutils literal"><span class="pre">TarError</span></tt></a> exceptions as well.</p>
<p>The <em>encoding</em> and <em>errors</em> arguments control the way strings are converted to
unicode objects and vice versa. The default settings will work for most users.
See section <a class="reference internal" href="#tar-unicode"><em>Unicode issues</em></a> for in-depth information.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
<p>The <em>pax_headers</em> argument is an optional dictionary of unicode strings which
will be added as a pax global header if <em>format</em> is <a title="tarfile.PAX_FORMAT" class="reference internal" href="#tarfile.PAX_FORMAT"><tt class="xref docutils literal"><span class="pre">PAX_FORMAT</span></tt></a>.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.open">
<tt class="descclassname">TarFile.</tt><tt class="descname">open</tt><big>(</big><em>...</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.open" title="Permalink to this definition">¶</a></dt>
<dd>Alternative constructor. The <a title="tarfile.open" class="reference internal" href="#tarfile.open"><tt class="xref docutils literal"><span class="pre">tarfile.open()</span></tt></a> function is actually a
shortcut to this classmethod.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.getmember">
<tt class="descclassname">TarFile.</tt><tt class="descname">getmember</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.getmember" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object for member <em>name</em>. If <em>name</em> can not be found
in the archive, <a title="exceptions.KeyError" class="reference external" href="exceptions.html#exceptions.KeyError"><tt class="xref docutils literal"><span class="pre">KeyError</span></tt></a> is raised.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If a member occurs more than once in the archive, its last occurrence is assumed
to be the most up-to-date version.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.getmembers">
<tt class="descclassname">TarFile.</tt><tt class="descname">getmembers</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarFile.getmembers" title="Permalink to this definition">¶</a></dt>
<dd>Return the members of the archive as a list of <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> objects. The
list has the same order as the members in the archive.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.getnames">
<tt class="descclassname">TarFile.</tt><tt class="descname">getnames</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarFile.getnames" title="Permalink to this definition">¶</a></dt>
<dd>Return the members as a list of their names. It has the same order as the list
returned by <a title="tarfile.TarFile.getmembers" class="reference internal" href="#tarfile.TarFile.getmembers"><tt class="xref docutils literal"><span class="pre">getmembers()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.list">
<tt class="descclassname">TarFile.</tt><tt class="descname">list</tt><big>(</big><em>verbose=True</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.list" title="Permalink to this definition">¶</a></dt>
<dd>Print a table of contents to <tt class="docutils literal"><span class="pre">sys.stdout</span></tt>. If <em>verbose</em> is <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>,
only the names of the members are printed. If it is <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>, output
similar to that of <strong>ls -l</strong> is produced.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.next">
<tt class="descclassname">TarFile.</tt><tt class="descname">next</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarFile.next" title="Permalink to this definition">¶</a></dt>
<dd>Return the next member of the archive as a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object, when
<a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> is opened for reading. Return <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> if there is no more
available.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.extractall">
<tt class="descclassname">TarFile.</tt><tt class="descname">extractall</tt><big>(</big><em>path=&quot;.&quot;</em>, <em>members=None</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.extractall" title="Permalink to this definition">¶</a></dt>
<dd><p>Extract all members from the archive to the current working directory or
directory <em>path</em>. If optional <em>members</em> is given, it must be a subset of the
list returned by <a title="tarfile.TarFile.getmembers" class="reference internal" href="#tarfile.TarFile.getmembers"><tt class="xref docutils literal"><span class="pre">getmembers()</span></tt></a>. Directory information like owner,
modification time and permissions are set after all members have been extracted.
This is done to work around two problems: A directory&#8217;s modification time is
reset each time a file is created in it. And, if a directory&#8217;s permissions do
not allow writing, extracting files to it will fail.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Never extract archives from untrusted sources without prior inspection.
It is possible that files are created outside of <em>path</em>, e.g. members
that have absolute filenames starting with <tt class="docutils literal"><span class="pre">&quot;/&quot;</span></tt> or filenames with two
dots <tt class="docutils literal"><span class="pre">&quot;..&quot;</span></tt>.</p>
</div>
<p>
<span class="versionmodified">New in version 2.5.</span></p>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.extract">
<tt class="descclassname">TarFile.</tt><tt class="descname">extract</tt><big>(</big><em>member</em>, <em>path=&quot;&quot;</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.extract" title="Permalink to this definition">¶</a></dt>
<dd><p>Extract a member from the archive to the current working directory, using its
full name. Its file information is extracted as accurately as possible. <em>member</em>
may be a filename or a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object. You can specify a different
directory using <em>path</em>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The <a title="tarfile.TarFile.extract" class="reference internal" href="#tarfile.TarFile.extract"><tt class="xref docutils literal"><span class="pre">extract()</span></tt></a> method does not take care of several extraction issues.
In most cases you should consider using the <a title="tarfile.TarFile.extractall" class="reference internal" href="#tarfile.TarFile.extractall"><tt class="xref docutils literal"><span class="pre">extractall()</span></tt></a> method.</p>
</div>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">See the warning for <a title="tarfile.TarFile.extractall" class="reference internal" href="#tarfile.TarFile.extractall"><tt class="xref docutils literal"><span class="pre">extractall()</span></tt></a>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.extractfile">
<tt class="descclassname">TarFile.</tt><tt class="descname">extractfile</tt><big>(</big><em>member</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.extractfile" title="Permalink to this definition">¶</a></dt>
<dd><p>Extract a member from the archive as a file object. <em>member</em> may be a filename
or a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object. If <em>member</em> is a regular file, a file-like object
is returned. If <em>member</em> is a link, a file-like object is constructed from the
link&#8217;s target. If <em>member</em> is none of the above, <a title="None" class="reference external" href="constants.html#None"><tt class="xref xref docutils literal"><span class="pre">None</span></tt></a> is returned.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The file-like object is read-only and provides the following methods:
<tt class="xref docutils literal"><span class="pre">read()</span></tt>, <tt class="xref docutils literal"><span class="pre">readline()</span></tt>, <tt class="xref docutils literal"><span class="pre">readlines()</span></tt>, <tt class="xref docutils literal"><span class="pre">seek()</span></tt>, <tt class="xref docutils literal"><span class="pre">tell()</span></tt>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.add">
<tt class="descclassname">TarFile.</tt><tt class="descname">add</tt><big>(</big><em>name</em>, <em>arcname=None</em>, <em>recursive=True</em>, <em>exclude=None</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Add the file <em>name</em> to the archive. <em>name</em> may be any type of file (directory,
fifo, symbolic link, etc.). If given, <em>arcname</em> specifies an alternative name
for the file in the archive. Directories are added recursively by default. This
can be avoided by setting <em>recursive</em> to <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>. If <em>exclude</em> is given
it must be a function that takes one filename argument and returns a boolean
value. Depending on this value the respective file is either excluded
(<a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a>) or added (<a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>).</p>
<p>
<span class="versionmodified">Changed in version 2.6: </span>Added the <em>exclude</em> parameter.</p>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.addfile">
<tt class="descclassname">TarFile.</tt><tt class="descname">addfile</tt><big>(</big><em>tarinfo</em>, <em>fileobj=None</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.addfile" title="Permalink to this definition">¶</a></dt>
<dd><p>Add the <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object <em>tarinfo</em> to the archive. If <em>fileobj</em> is given,
<tt class="docutils literal"><span class="pre">tarinfo.size</span></tt> bytes are read from it and added to the archive.  You can
create <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> objects using <a title="tarfile.TarFile.gettarinfo" class="reference internal" href="#tarfile.TarFile.gettarinfo"><tt class="xref docutils literal"><span class="pre">gettarinfo()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">On Windows platforms, <em>fileobj</em> should always be opened with mode <tt class="docutils literal"><span class="pre">'rb'</span></tt> to
avoid irritation about the file size.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.gettarinfo">
<tt class="descclassname">TarFile.</tt><tt class="descname">gettarinfo</tt><big>(</big><em>name=None</em>, <em>arcname=None</em>, <em>fileobj=None</em><big>)</big><a class="headerlink" href="#tarfile.TarFile.gettarinfo" title="Permalink to this definition">¶</a></dt>
<dd>Create a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object for either the file <em>name</em> or the file object
<em>fileobj</em> (using <a title="os.fstat" class="reference external" href="os.html#os.fstat"><tt class="xref docutils literal"><span class="pre">os.fstat()</span></tt></a> on its file descriptor).  You can modify some
of the <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a>&#8216;s attributes before you add it using <a title="tarfile.TarFile.addfile" class="reference internal" href="#tarfile.TarFile.addfile"><tt class="xref docutils literal"><span class="pre">addfile()</span></tt></a>.
If given, <em>arcname</em> specifies an alternative name for the file in the archive.</dd></dl>

<dl class="method">
<dt id="tarfile.TarFile.close">
<tt class="descclassname">TarFile.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarFile.close" title="Permalink to this definition">¶</a></dt>
<dd>Close the <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a>. In write mode, two finishing zero blocks are
appended to the archive.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarFile.posix">
<tt class="descclassname">TarFile.</tt><tt class="descname">posix</tt><a class="headerlink" href="#tarfile.TarFile.posix" title="Permalink to this definition">¶</a></dt>
<dd><p>Setting this to <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> is equivalent to setting the <a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format</span></tt></a>
attribute to <a title="tarfile.USTAR_FORMAT" class="reference internal" href="#tarfile.USTAR_FORMAT"><tt class="xref docutils literal"><span class="pre">USTAR_FORMAT</span></tt></a>, <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a> is equivalent to
<a title="tarfile.GNU_FORMAT" class="reference internal" href="#tarfile.GNU_FORMAT"><tt class="xref docutils literal"><span class="pre">GNU_FORMAT</span></tt></a>.</p>
<p>
<span class="versionmodified">Changed in version 2.4: </span><em>posix</em> defaults to <a title="False" class="reference external" href="constants.html#False"><tt class="xref xref docutils literal"><span class="pre">False</span></tt></a>.</p>
<p>
<span class="versionmodified">Deprecated since version 2.6: </span>Use the <a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format</span></tt></a> attribute instead.</p>
</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarFile.pax_headers">
<tt class="descclassname">TarFile.</tt><tt class="descname">pax_headers</tt><a class="headerlink" href="#tarfile.TarFile.pax_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary containing key-value pairs of pax global headers.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

</div>
<div class="section" id="tarinfo-objects">
<span id="id2"></span><h2>13.5.2. TarInfo Objects<a class="headerlink" href="#tarinfo-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object represents one member in a <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a>. Aside
from storing all required attributes of a file (like file type, size, time,
permissions, owner etc.), it provides some useful methods to determine its type.
It does <em>not</em> contain the file&#8217;s data itself.</p>
<p><a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> objects are returned by <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a>&#8216;s methods
<tt class="xref docutils literal"><span class="pre">getmember()</span></tt>, <tt class="xref docutils literal"><span class="pre">getmembers()</span></tt> and <tt class="xref docutils literal"><span class="pre">gettarinfo()</span></tt>.</p>
<dl class="class">
<dt id="tarfile.TarInfo">
<em class="property">
class </em><tt class="descclassname">tarfile.</tt><tt class="descname">TarInfo</tt><big>(</big><em>name=&quot;&quot;</em><big>)</big><a class="headerlink" href="#tarfile.TarInfo" title="Permalink to this definition">¶</a></dt>
<dd>Create a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.frombuf">
<tt class="descclassname">TarInfo.</tt><tt class="descname">frombuf</tt><big>(</big><em>buf</em><big>)</big><a class="headerlink" href="#tarfile.TarInfo.frombuf" title="Permalink to this definition">¶</a></dt>
<dd><p>Create and return a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object from string buffer <em>buf</em>.</p>
<p>
<span class="versionmodified">New in version 2.6: </span>Raises <a title="tarfile.HeaderError" class="reference internal" href="#tarfile.HeaderError"><tt class="xref docutils literal"><span class="pre">HeaderError</span></tt></a> if the buffer is invalid..</p>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.fromtarfile">
<tt class="descclassname">TarInfo.</tt><tt class="descname">fromtarfile</tt><big>(</big><em>tarfile</em><big>)</big><a class="headerlink" href="#tarfile.TarInfo.fromtarfile" title="Permalink to this definition">¶</a></dt>
<dd><p>Read the next member from the <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> object <em>tarfile</em> and return it as
a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.tobuf">
<tt class="descclassname">TarInfo.</tt><tt class="descname">tobuf</tt><big>(</big><em>format=DEFAULT_FORMAT</em>, <em>encoding=ENCODING</em>, <em>errors='strict'</em><big>)</big><a class="headerlink" href="#tarfile.TarInfo.tobuf" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a string buffer from a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object. For information on the
arguments see the constructor of the <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> class.</p>
<p>
<span class="versionmodified">Changed in version 2.6: </span>The arguments were added.</p>
</dd></dl>

<p>A <tt class="docutils literal"><span class="pre">TarInfo</span></tt> object has the following public data attributes:</p>
<dl class="attribute">
<dt id="tarfile.TarInfo.name">
<tt class="descclassname">TarInfo.</tt><tt class="descname">name</tt><a class="headerlink" href="#tarfile.TarInfo.name" title="Permalink to this definition">¶</a></dt>
<dd>Name of the archive member.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.size">
<tt class="descclassname">TarInfo.</tt><tt class="descname">size</tt><a class="headerlink" href="#tarfile.TarInfo.size" title="Permalink to this definition">¶</a></dt>
<dd>Size in bytes.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.mtime">
<tt class="descclassname">TarInfo.</tt><tt class="descname">mtime</tt><a class="headerlink" href="#tarfile.TarInfo.mtime" title="Permalink to this definition">¶</a></dt>
<dd>Time of last modification.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.mode">
<tt class="descclassname">TarInfo.</tt><tt class="descname">mode</tt><a class="headerlink" href="#tarfile.TarInfo.mode" title="Permalink to this definition">¶</a></dt>
<dd>Permission bits.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.type">
<tt class="descclassname">TarInfo.</tt><tt class="descname">type</tt><a class="headerlink" href="#tarfile.TarInfo.type" title="Permalink to this definition">¶</a></dt>
<dd>File type.  <em>type</em> is usually one of these constants: <tt class="xref docutils literal"><span class="pre">REGTYPE</span></tt>,
<tt class="xref docutils literal"><span class="pre">AREGTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">LNKTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">SYMTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">DIRTYPE</span></tt>,
<tt class="xref docutils literal"><span class="pre">FIFOTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">CONTTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">CHRTYPE</span></tt>, <tt class="xref docutils literal"><span class="pre">BLKTYPE</span></tt>,
<tt class="xref docutils literal"><span class="pre">GNUTYPE_SPARSE</span></tt>.  To determine the type of a <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object
more conveniently, use the <tt class="docutils literal"><span class="pre">is_*()</span></tt> methods below.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.linkname">
<tt class="descclassname">TarInfo.</tt><tt class="descname">linkname</tt><a class="headerlink" href="#tarfile.TarInfo.linkname" title="Permalink to this definition">¶</a></dt>
<dd>Name of the target file name, which is only present in <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> objects
of type <tt class="xref docutils literal"><span class="pre">LNKTYPE</span></tt> and <tt class="xref docutils literal"><span class="pre">SYMTYPE</span></tt>.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.uid">
<tt class="descclassname">TarInfo.</tt><tt class="descname">uid</tt><a class="headerlink" href="#tarfile.TarInfo.uid" title="Permalink to this definition">¶</a></dt>
<dd>User ID of the user who originally stored this member.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.gid">
<tt class="descclassname">TarInfo.</tt><tt class="descname">gid</tt><a class="headerlink" href="#tarfile.TarInfo.gid" title="Permalink to this definition">¶</a></dt>
<dd>Group ID of the user who originally stored this member.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.uname">
<tt class="descclassname">TarInfo.</tt><tt class="descname">uname</tt><a class="headerlink" href="#tarfile.TarInfo.uname" title="Permalink to this definition">¶</a></dt>
<dd>User name.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.gname">
<tt class="descclassname">TarInfo.</tt><tt class="descname">gname</tt><a class="headerlink" href="#tarfile.TarInfo.gname" title="Permalink to this definition">¶</a></dt>
<dd>Group name.</dd></dl>

<dl class="attribute">
<dt id="tarfile.TarInfo.pax_headers">
<tt class="descclassname">TarInfo.</tt><tt class="descname">pax_headers</tt><a class="headerlink" href="#tarfile.TarInfo.pax_headers" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary containing key-value pairs of an associated pax extended header.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<p>A <a title="tarfile.TarInfo" class="reference internal" href="#tarfile.TarInfo"><tt class="xref docutils literal"><span class="pre">TarInfo</span></tt></a> object also provides some convenient query methods:</p>
<dl class="method">
<dt id="tarfile.TarInfo.isfile">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isfile</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isfile" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if the <tt class="xref docutils literal"><span class="pre">Tarinfo</span></tt> object is a regular file.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.isreg">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isreg</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isreg" title="Permalink to this definition">¶</a></dt>
<dd>Same as <a title="tarfile.TarInfo.isfile" class="reference internal" href="#tarfile.TarInfo.isfile"><tt class="xref docutils literal"><span class="pre">isfile()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.isdir">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isdir</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isdir" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a directory.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.issym">
<tt class="descclassname">TarInfo.</tt><tt class="descname">issym</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.issym" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a symbolic link.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.islnk">
<tt class="descclassname">TarInfo.</tt><tt class="descname">islnk</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.islnk" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a hard link.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.ischr">
<tt class="descclassname">TarInfo.</tt><tt class="descname">ischr</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.ischr" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a character device.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.isblk">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isblk</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isblk" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a block device.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.isfifo">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isfifo</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isfifo" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is a FIFO.</dd></dl>

<dl class="method">
<dt id="tarfile.TarInfo.isdev">
<tt class="descclassname">TarInfo.</tt><tt class="descname">isdev</tt><big>(</big><big>)</big><a class="headerlink" href="#tarfile.TarInfo.isdev" title="Permalink to this definition">¶</a></dt>
<dd>Return <a title="True" class="reference external" href="constants.html#True"><tt class="xref xref docutils literal"><span class="pre">True</span></tt></a> if it is one of character device, block device or FIFO.</dd></dl>

</div>
<div class="section" id="examples">
<span id="tar-examples"></span><h2>13.5.3. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>How to extract an entire tar archive to the current working directory:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&quot;sample.tar.gz&quot;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>How to extract a subset of a tar archive with <a title="tarfile.TarFile.extractall" class="reference internal" href="#tarfile.TarFile.extractall"><tt class="xref docutils literal"><span class="pre">TarFile.extractall()</span></tt></a> using
a generator function instead of a list:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">tarfile</span>

<span class="k">def</span> <span class="nf">py_files</span><span class="p">(</span><span class="n">members</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">members</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">)[</span><span class="mf">1</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;.py&quot;</span><span class="p">:</span>
            <span class="k">yield</span> <span class="n">tarinfo</span>

<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&quot;sample.tar.gz&quot;</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">(</span><span class="n">members</span><span class="o">=</span><span class="n">py_files</span><span class="p">(</span><span class="n">tar</span><span class="p">))</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>How to create an uncompressed tar archive from a list of filenames:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&quot;sample.tar&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;bar&quot;</span><span class="p">,</span> <span class="s">&quot;quux&quot;</span><span class="p">]:</span>
    <span class="n">tar</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>How to read a gzip compressed tar archive and display some member information:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">tarfile</span>
<span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&quot;sample.tar.gz&quot;</span><span class="p">,</span> <span class="s">&quot;r:gz&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">tarinfo</span> <span class="ow">in</span> <span class="n">tar</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="s">&quot;is&quot;</span><span class="p">,</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">size</span><span class="p">,</span> <span class="s">&quot;bytes in size and is&quot;</span><span class="p">,</span>
    <span class="k">if</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isreg</span><span class="p">():</span>
        <span class="k">print</span> <span class="s">&quot;a regular file.&quot;</span>
    <span class="k">elif</span> <span class="n">tarinfo</span><span class="o">.</span><span class="n">isdir</span><span class="p">():</span>
        <span class="k">print</span> <span class="s">&quot;a directory.&quot;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">print</span> <span class="s">&quot;something else.&quot;</span>
<span class="n">tar</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="supported-tar-formats">
<span id="tar-formats"></span><h2>13.5.4. Supported tar formats<a class="headerlink" href="#supported-tar-formats" title="Permalink to this headline">¶</a></h2>
<p>There are three tar formats that can be created with the <tt class="xref docutils literal"><span class="pre">tarfile</span></tt> module:</p>
<ul>
<li><p class="first">The POSIX.1-1988 ustar format (<a title="tarfile.USTAR_FORMAT" class="reference internal" href="#tarfile.USTAR_FORMAT"><tt class="xref docutils literal"><span class="pre">USTAR_FORMAT</span></tt></a>). It supports filenames
up to a length of at best 256 characters and linknames up to 100 characters. The
maximum file size is 8 gigabytes. This is an old and limited but widely
supported format.</p>
</li>
<li><p class="first">The GNU tar format (<a title="tarfile.GNU_FORMAT" class="reference internal" href="#tarfile.GNU_FORMAT"><tt class="xref docutils literal"><span class="pre">GNU_FORMAT</span></tt></a>). It supports long filenames and
linknames, files bigger than 8 gigabytes and sparse files. It is the de facto
standard on GNU/Linux systems. <tt class="xref docutils literal"><span class="pre">tarfile</span></tt> fully supports the GNU tar
extensions for long names, sparse file support is read-only.</p>
</li>
<li><p class="first">The POSIX.1-2001 pax format (<a title="tarfile.PAX_FORMAT" class="reference internal" href="#tarfile.PAX_FORMAT"><tt class="xref docutils literal"><span class="pre">PAX_FORMAT</span></tt></a>). It is the most flexible
format with virtually no limits. It supports long filenames and linknames, large
files and stores pathnames in a portable way. However, not all tar
implementations today are able to handle pax archives properly.</p>
<p>The <em>pax</em> format is an extension to the existing <em>ustar</em> format. It uses extra
headers for information that cannot be stored otherwise. There are two flavours
of pax headers: Extended headers only affect the subsequent file header, global
headers are valid for the complete archive and affect all following files. All
the data in a pax header is encoded in <em>UTF-8</em> for portability reasons.</p>
</li>
</ul>
<p>There are some more variants of the tar format which can be read, but not
created:</p>
<ul class="simple">
<li>The ancient V7 format. This is the first tar format from Unix Seventh Edition,
storing only regular files and directories. Names must not be longer than 100
characters, there is no user/group name information. Some archives have
miscalculated header checksums in case of fields with non-ASCII characters.</li>
<li>The SunOS tar extended format. This format is a variant of the POSIX.1-2001
pax format, but is not compatible.</li>
</ul>
</div>
<div class="section" id="unicode-issues">
<span id="tar-unicode"></span><h2>13.5.5. Unicode issues<a class="headerlink" href="#unicode-issues" title="Permalink to this headline">¶</a></h2>
<p>The tar format was originally conceived to make backups on tape drives with the
main focus on preserving file system information. Nowadays tar archives are
commonly used for file distribution and exchanging archives over networks. One
problem of the original format (that all other formats are merely variants of)
is that there is no concept of supporting different character encodings. For
example, an ordinary tar archive created on a <em>UTF-8</em> system cannot be read
correctly on a <em>Latin-1</em> system if it contains non-ASCII characters. Names (i.e.
filenames, linknames, user/group names) containing these characters will appear
damaged.  Unfortunately, there is no way to autodetect the encoding of an
archive.</p>
<p>The pax format was designed to solve this problem. It stores non-ASCII names
using the universal character encoding <em>UTF-8</em>. When a pax archive is read,
these <em>UTF-8</em> names are converted to the encoding of the local file system.</p>
<p>The details of unicode conversion are controlled by the <em>encoding</em> and <em>errors</em>
keyword arguments of the <a title="tarfile.TarFile" class="reference internal" href="#tarfile.TarFile"><tt class="xref docutils literal"><span class="pre">TarFile</span></tt></a> class.</p>
<p>The default value for <em>encoding</em> is the local character encoding. It is deduced
from <a title="sys.getfilesystemencoding" class="reference external" href="sys.html#sys.getfilesystemencoding"><tt class="xref docutils literal"><span class="pre">sys.getfilesystemencoding()</span></tt></a> and <a title="sys.getdefaultencoding" class="reference external" href="sys.html#sys.getdefaultencoding"><tt class="xref docutils literal"><span class="pre">sys.getdefaultencoding()</span></tt></a>. In
read mode, <em>encoding</em> is used exclusively to convert unicode names from a pax
archive to strings in the local character encoding. In write mode, the use of
<em>encoding</em> depends on the chosen archive format. In case of <a title="tarfile.PAX_FORMAT" class="reference internal" href="#tarfile.PAX_FORMAT"><tt class="xref docutils literal"><span class="pre">PAX_FORMAT</span></tt></a>,
input names that contain non-ASCII characters need to be decoded before being
stored as <em>UTF-8</em> strings. The other formats do not make use of <em>encoding</em>
unless unicode objects are used as input names. These are converted to 8-bit
character strings before they are added to the archive.</p>
<p>The <em>errors</em> argument defines how characters are treated that cannot be
converted to or from <em>encoding</em>. Possible values are listed in section
<a class="reference external" href="codecs.html#codec-base-classes"><em>Codec Base Classes</em></a>. In read mode, there is an additional scheme
<tt class="docutils literal"><span class="pre">'utf-8'</span></tt> which means that bad characters are replaced by their <em>UTF-8</em>
representation. This is the default scheme. In write mode the default value for
<em>errors</em> is <tt class="docutils literal"><span class="pre">'strict'</span></tt> to ensure that name information is not altered
unnoticed.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">13.5. <tt class="docutils literal"><span class="pre">tarfile</span></tt> &#8212; Read and write tar archive files</a><ul>
<li><a class="reference external" href="#tarfile-objects">13.5.1. TarFile Objects</a></li>
<li><a class="reference external" href="#tarinfo-objects">13.5.2. TarInfo Objects</a></li>
<li><a class="reference external" href="#examples">13.5.3. Examples</a></li>
<li><a class="reference external" href="#supported-tar-formats">13.5.4. Supported tar formats</a></li>
<li><a class="reference external" href="#unicode-issues">13.5.5. Unicode issues</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="zipfile.html"
                                  title="previous chapter">13.4. <tt class="docutils literal docutils literal"><span class="pre">zipfile</span></tt> &#8212; Work with ZIP archives</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="fileformats.html"
                                  title="next chapter">14. File Formats</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/tarfile.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="fileformats.html" title="14. File Formats"
             >next</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="13.4. zipfile — Work with ZIP archives"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="archiving.html" >13. Data Compression and Archiving</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>